package com.qianclass.talklive.app.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;

import com.qianclass.talklive.R;
import com.qianclass.talklive.app.utils.DensityUtil;
import com.qianclass.talklive.app.utils.LogUtil;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ExpressionUtil {
	private static String utilstr;
	
	/**
	 * 对spanableString进行正则判断，如果符合要求，则以表情图片代替
	 * @param context
	 * @param spannableString
	 * @param patten
	 * @param start
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws NumberFormatException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
    @SuppressWarnings("deprecation")
	public static void dealExpression(Context context,SpannableString spannableString, Pattern patten, int start) throws SecurityException, NoSuchFieldException, NumberFormatException, IllegalArgumentException, IllegalAccessException {
    	Matcher matcher = patten.matcher(spannableString);
    	
        while (matcher.find()) {
            String key = matcher.group();
            if (matcher.start() < start) {
                continue;
            }
            int a = key.indexOf("</");
			int b = key.indexOf("/>");

			utilstr = key.substring(a + 2, b);
            
            Field field = R.drawable.class.getDeclaredField("f"+utilstr);
			int resId = Integer.parseInt(field.get(null).toString());		//通过上面匹配得到的字符串来生成图片资源id
            if (resId != 0) {
                //Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
				//ImageSpan iSpan = new ImageSpan(context, bitmap);				//通过图片资源id来得到bitmap，用一个ImageSpan来包装
                Drawable drawable = context.getResources().getDrawable(resId);
                drawable.setBounds(0, 0, DensityUtil.dip2px(context, 20), DensityUtil.dip2px(context, 20));
                ImageSpan iSpan = new ImageSpan(drawable);
                int end = matcher.start() + key.length();					//计算该图片名字的长度，也就是要替换的字符串的长度
                spannableString.setSpan(iSpan, matcher.start(), end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);	//将该图片替换字符串中规定的位置中
                if (end < spannableString.length()) {						//如果整个字符串还未验证完，则继续。。
                    dealExpression(context,spannableString,  patten, end);

                }
                break;
            }
        }
    }
    
    /**
     * 得到一个SpanableString对象，通过传入的字符串,并进行正则判断
     * @param context
     * @param str
     * @return
     */
    public static SpannableString getExpressionString(Context context,String str,String zhengze){
    	SpannableString spannableString = new SpannableString(str);
        Pattern sinaPatten = Pattern.compile(zhengze, Pattern.CASE_INSENSITIVE);		//通过传入的正则表达式来生成一个pattern
        try {
            dealExpression(context,spannableString, sinaPatten, 0);
        } catch (Exception e) {
            LogUtil.e("dealExpression", e.getMessage());
        }
        return spannableString;
    }
	

}